﻿// Copyright (C) 2009  Lex Li
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

/*
 * Created by SharpDevelop.
 * Date: 10/1/2009
 * Time: 4:54 PM
 */
using Lextm.M8Pack.Components.Properties;

namespace Lextm.M8Pack.Components
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    
    /// <summary>
    /// Description of ButtonIcon.
    /// </summary>
    public class AppIcon : DrawingArea
    {
        private readonly Shortcut _shortcut;
        private readonly OpenFileDialog _ofdIcon;   
        private string _name = string.Empty;
        private string _display = string.Empty;
        private Image _image;
        private string _fontName = Resources.LabelFontName;
        private const float FontSize = 12f;
        
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
        public AppIcon()
        {
            Height = 130;
            Width = 110;

            _shortcut = new Shortcut();
            _shortcut.Image = Resources.empty;
            _shortcut.Left = Width - _shortcut.Width;
            _shortcut.Top = Height - _shortcut.Height;
            _shortcut.Click += ShortcutClick;
            Controls.Add(_shortcut);

            _ofdIcon = new OpenFileDialog();
            _ofdIcon.Filter = Resources.PngImagesFilter;
            _ofdIcon.Title = Resources.PngImageSelection;
        }
        
        private bool IconIsEmpty
        {
            get { return _image == null; }
        }

        private void ShortcutClick(object sender, EventArgs e)
        {
            if (IconIsEmpty)
            {
                return;
            }
            
            EventHandler<ImageBrowsedEventArgs> handler = ShortcutClicked;
            if (handler != null)
            {
                if (_ofdIcon.ShowDialog() == DialogResult.OK)
                {
                    handler(this, new ImageBrowsedEventArgs(_ofdIcon.FileName));
                }
            }
        }
        
        protected override void OnInvalidated(InvalidateEventArgs e)
        {
            base.OnInvalidated(e);
            _shortcut.Invalidate();
        }
        
        public event EventHandler<ImageBrowsedEventArgs> ShortcutClicked;
        
        [Browsable(true)]
        public string DisplayName
        {
            get { return _display; }
            set
            {
                _display = value ?? string.Empty;
                Invalidate();
            }
        }
        
        [Browsable(true)]
        public string LabelName
        {
            get { return _name; }
            set
            {
                _name = value ?? string.Empty;
                Invalidate();
            }
        }

        [Bindable(true)]
        public Image Image
        {
            get { return _image; }
            set
            {
                _image = value;
                _shortcut.Image = _image == null ? Resources.empty: Resources.document_open;
                Invalidate();
            }
        }

        public string LabelFont
        {
            get {
                return _fontName;
            }
            set {
                _fontName = value;
                Invalidate();
            }
        }

        protected override void OnDraw(Graphics graphics)
        {
            if (!Visible)
            {
                return;
            }
            
            DrawOn(graphics, 0, 0);
        }

        public void DrawOn(Graphics graphics, int left, int top)
        {
            if (string.IsNullOrEmpty(_display))
            {
                return;
            }
            
            Rectangle textRegion = new Rectangle(left + 10, top + 110, 90, 24);
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            graphics.DrawString(_display, new Font(_fontName, FontSize), Brushes.White, textRegion, format);

            if (_image == null)
            {
                return;
            }

            // Sets the images' sizes and positions
            Rectangle imageRegion = new Rectangle(left + 10, top + 10, 90, 90);

            // Draws the two images
            graphics.DrawImage(_image, imageRegion);
        }

        public void SwitchMode()
        {
            _shortcut.Visible = !_shortcut.Visible;
        }
    }
}
